﻿using Abp.BackgroundJobs;
using Abp.Dependency;
using System;
using System.Collections.Generic;
using System.Text;
using Abp.Domain.Repositories;
using FwProject.Core.SettlementInfoModel;
using System.Linq;
using Abp.Domain.Uow;
using Castle.Core.Logging;
using Abp.Threading.Timers;
using Abp.Threading.BackgroundWorkers;

namespace FwProject.BackGround.Settlement
{
    public class SettlementPayWorkBackGround : PeriodicBackgroundWorkerBase,ISingletonDependency
    {
        private readonly IRepository<SettlementInfo,Guid> _repositorySettlementInfo;
        public ILogger logger { get; set; }
        public SettlementPayWorkBackGround(AbpTimer timer, IRepository<SettlementInfo, Guid> repositorySettlementInfo):base(timer) {

#if DEBUG
            Timer.Period = 1000 * 60;
#else
           Timer.Period = 1000 * 60 * 60 * 24;
#endif
            _repositorySettlementInfo = repositorySettlementInfo;
            logger = NullLogger.Instance;
        }
        [UnitOfWork]
        protected override void DoWork()
        {
            using (CurrentUnitOfWork.DisableFilter(AbpDataFilters.MayHaveTenant))
            {
                logger.Info("后台任务开始处理收款确认");
                var list = _repositorySettlementInfo.GetAll().Where(o => o.RestaurantPayStatus == RestaurantPayStatus.未收款 && o.CreationTime.AddDays(1) <= DateTime.Now);
                logger.Info($"后台任务开始处理收款确认{list.Count()}需要处理");
                foreach (var item in list)
                {
                    //item.RestaurantPayStatus = RestaurantPayStatus.已确认;
                }
                logger.Info($"后台任务开始处理收款确认处理完成");

                CurrentUnitOfWork.SaveChanges();
            }
        }
    }
}
